<?php

/**
 * @link https://github.com/BigKuCha/yii2-ueditor-widget
 * @link http://ueditor.baidu.com/website/index.html
 */

namespace common\widgets\ueditor;

use Yii;
use yii\web\View;
use yii\widgets\InputWidget;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\helpers\Html;

class UEditor extends InputWidget {

    //配置选项，参阅Ueditor官网文档(定制菜单等)
    public $clientOptions = [
        'toolbars' => [
            [
                'fullscreen', 'source', '|',
                'undo', 'redo', '|',
                'bold', 'italic', 'fontborder', 'horizontal', 'removeformat', 'formatmatch', '|',
                'forecolor', 'insertorderedlist', 'insertunorderedlist', '|',
                'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
                'link', 'unlink', '|', 'insertvideo', 'video',
                'simpleupload', 'insertimage', '|',
                'pasteplain', 'selectall', 'cleardoc',
            ],
        ],
        'wordCount' => false,
        'theme' => 'pithy',
        'elementPathEnabled' => false,
        'enableAutoSave' => false,
        'zIndex' => 9990,
        'scaleEnabled' => true,
    ];
    //默认配置
    protected $_options;

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function init() {
        if (isset($this->options['id'])) {
            $this->id = $this->options['id'];
        } else {
            $this->id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id;
        }
        $this->_options = [
            'serverUrl' => Url::to(['/attachs/ueditor']),
            'initialFrameWidth' => '100%',
            'initialFrameHeight' => '400',
            'lang' => (strtolower(Yii::$app->language) == 'en-us') ? 'en' : 'zh-cn',
        ];
        $this->clientOptions = ArrayHelper::merge($this->_options, $this->clientOptions);
        parent::init();
    }

    public function run() {
        $this->registerClientScript();
        if ($this->hasModel()) {
            return Html::activeTextarea($this->model, $this->attribute, ['id' => $this->id]);
        } else {
            return Html::textarea($this->id, $this->value, ['id' => $this->id]);
        }
    }

    /**
     * 注册客户端脚本
     */
    protected function registerClientScript() {
        UEditorAsset::register($this->view);
        $clientOptions = Json::encode($this->clientOptions);
        $script = "UE.getEditor('" . $this->id . "', " . $clientOptions . ");";
        $this->view->registerJs($script, View::POS_READY);
    }

}
